﻿#include "DataExportor.h"
#include <QDebug>

DataExportor::DataExportor(QObject *parent)
	: QObject(parent)
{
	m_asMicExcel = new AsMicrosoftExcel(AsMicrosoftExcel::Visible);
	m_asMicExcel->initExcelApplication();
	for (int i = 0;i<200;i++)
	{
		m_vec << i;
	}
}

DataExportor::~DataExportor()
{
	delete m_asMicExcel;
}

void DataExportor::exportDataToMicrosoftExcel()
{
	if (!m_asMicExcel->openWorkBook("HighSpeedSignalDatas.xlsx"))
	{
		qDebug() << "create a new excel file";
		m_asMicExcel->addWorkBook("HighSpeedSignalDatas.xlsx","PCIDATA");
		m_asMicExcel->saveWorkBook("HighSpeedSignalDatas.xlsx");
	}
	//for (int i = 0;i<m_vec.size();i++)
	//{
	//	qDebug() << m_asMicExcel->setCell(i+1, 1, m_vec[i],0,0);
	//}
	//m_asMicExcel->saveWorkBook();

	qDebug() << "正在导出数据到Excel......";

	QMap<int, QVector<cv::Mat>> dataMap = PtrCoreData->getMatDatasMap();
	if (dataMap.isEmpty())
	{
		qDebug() << "没有可用数据，请采集成功后再进行导出......";
		return;
	}
	QMap<int, QVector<cv::Mat>>::const_iterator it = dataMap.constBegin();
	int row = 1, col = 1;
	while (it != dataMap.constEnd())
	{
		QVector<cv::Mat> matVec = it.value();
		for (int i = 0;i<matVec.size();i++)
		{
			m_asMicExcel->setCell(row, 1, QString("第%1个矩阵数据：").arg(i+1));
			for (int j = 0;j<ONE_MAT_ROW;j++)
			{
				float *ptr = matVec[i].ptr<float>(j);
				row++;
				for (int k = 0;k<ONCE_DATA_LENGTH;k++)
				{
					m_asMicExcel->setCell(row, k, m_vec[k], 0, 0);
				}
			}
			
		}
		++it;

	}

	m_asMicExcel->saveWorkBook();
	qDebug() << "数据导出完成！！！";
}
